package com.yhsimon.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TTLDirectRabbitMqConfiguration {

    //1.声明注册direct模式的交换机
    @Bean
    public DirectExchange ttlDirectExchange(){
        return new DirectExchange("ttl_direct_exchange",true,false);
    }

    //2.声明队列
    @Bean
    public Queue ttlDirectQueue(){

        Map<String, Object> args = new HashMap<>();
        //设置过期时间
        args.put("x-message-ttl",5000);
        //显示消息个数
        args.put("x-max-length",5);
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead");//fanout不需要配置
        return new Queue("ttl.direct.queue",true,false,false,args);
    }

    //2.声明ttl message队列
    @Bean
    public Queue ttlMessageQueue(){
        return new Queue("ttl.message.direct.queue",true);
    }

    //3.完成绑定关系（队列与交换机完成绑定关系）
    @Bean
    public Binding ttlDirectBinding(){
        return BindingBuilder.bind(ttlDirectQueue()).to(ttlDirectExchange()).with("ttl");
    }

    //3.完成绑定关系 ttl message
    @Bean
    public Binding ttlMessageBinding(){
        return BindingBuilder.bind(ttlMessageQueue()).to(ttlDirectExchange()).with("ttlMessage");
    }
}
